package com.qiantu.phone.ui.activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.hjq.base.DensityUtils;
import com.hjq.http.listener.HttpCallback;
import com.lxj.xpopup.XPopup;
import com.qiantu.api.db.LLFloorDBManager;
import com.qiantu.api.entity.DeviceBaseConfigInfo;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.DeviceBindingData;
import com.qiantu.api.entity.FloorBean;
import com.qiantu.api.entity.PanelBindConfig;
import com.qiantu.api.entity.PanelBindStatusBean;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.api.utils.DeviceInfoParseUtil;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.dialog.AreaSelectDialog;
import com.qiantu.phone.ui.fragment.DeviceListFragment;
import com.qiantu.phone.ui.fragment.SceneFragment;
import com.qiantu.phone.ui.fragment.SmartAppliancesFragment;
import com.qiantu.phone.ui.uiutil.DeviceMainType;
import com.qiantu.phone.ui.uiutil.DeviceType;
import com.qiantu.phone.widget.OperateBindView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KGXPanelBindDeviceActivity extends AppActivity {
    private SceneFragment mSceneFragment;
    private SmartAppliancesFragment mSmartAppliancesFragment;
    private String type;
    private List<PanelBindConfig> configData;
    private List<PanelBindConfig> orignConfigData;
    private int count;
    private int fromPos;//kgx壳子路数
    @Override
    protected int getLayoutId() {
        return R.layout.activity_kgxpanel_bind_device;
    }

    @Override
    protected void initView() {
        type = getString("type");
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (type.equals("scene")){
            mSceneFragment = SceneFragment.newInstance(false, true, SceneFragment.SELECT_SCENE,2);
            fragmentTransaction.add(R.id.fragment, mSceneFragment);
        }else {
            mSmartAppliancesFragment = SmartAppliancesFragment.newInstance(SmartAppliancesFragment.SearchType.SEARCH_TYPE_DEVICETYPE,type,4);
            mSmartAppliancesFragment.setSelectable(true);
            mSmartAppliancesFragment.setSingleSelect(false);
            fragmentTransaction.add(R.id.fragment, mSmartAppliancesFragment);
        }
        fragmentTransaction.commitNowAllowingStateLoss();
    }

    @Override
    protected void initData() {
        orignConfigData = getSerializable("data");
        configData = getSerializable("data");
        count = getInt("count");
        fromPos = getInt("fromPos");
        Map<String,Integer> indexList = new HashMap<>();
        for(int i = 0;i < configData.size();i++){
            PanelBindConfig config = configData.get(i);
//            indexList.put(config.getBindSerialNo(),config.getPanelIndex());
            indexList.put(config.getBindSerialNo(),1);
        }
        if (type.equals("scene")){
            mSceneFragment.setSelectData(indexList);
        }else{
            mSmartAppliancesFragment.setSelectData(indexList);
        }
    }

    //kgx只能添加一个
    public void addDevice(DeviceBean bean){
        configData.clear();
        int panelIndex = fromPos;
            PanelBindConfig newConfig = new PanelBindConfig();
            newConfig.setBindType(getDeviceBindType(bean));
            if(bean.getDeviceGroup() != 1){
                newConfig.setBindSerialNo(bean.getDeviceSerialNo());
            }else{
                newConfig.setBindType(12);
                newConfig.setBindSerialNo(bean.getDeviceGroupSerialNo());
            }
            newConfig.setPanelSalveSn(getString("controllerSerialNo"));
            newConfig.setBindUIType(getInt("bindUIType"));
            newConfig.setDeviceName(bean.getName());
            newConfig.setHouseSerialNo(AppApplication.getInstance().getCurrentHouseSerialNo());
            newConfig.setPanelIndex(panelIndex);
            configData.add(newConfig);
            mSmartAppliancesFragment.setSortData(bean.getDeviceSerialNo(),1);//这里写死为1

    }

    public void addScene(SceneBean bean){
        int index = checkSameDevice(bean.getSceneSerialNo());
        if(index == -1){
            if(configData.size() >= count){
                return;
            }
            Boolean isAdd = false;
            PanelBindConfig newConfig = new PanelBindConfig();
            newConfig.setBindType(7);
            newConfig.setBindSerialNo(bean.getSceneSerialNo());
            newConfig.setPanelSalveSn(getString("controllerSerialNo"));
            newConfig.setBindUIType(getInt("bindUIType"));
            newConfig.setDeviceName(bean.getName());
            newConfig.setHouseSerialNo(AppApplication.getInstance().getCurrentHouseSerialNo());
            Integer panelIndex = -1;
            for(int i = 0;i < count;i++){
                boolean isHave = false;
                for (PanelBindConfig config : configData){
                    if(config.getPanelIndex() == (i + 1)){
                        isHave = true;
                        break;
                    }
                }
                if(!isHave){
                    panelIndex = i + 1;
                    break;
                }
            }
            if(panelIndex >= configData.size()){
                configData.add(newConfig);
            }else{
                configData.add(panelIndex,newConfig);
            }
            newConfig.setPanelIndex(panelIndex);
            mSceneFragment.setSortData(bean.getSceneSerialNo(),panelIndex);
        }
    }
    public void removeData(String serialNo){
        for(int i = 0;i < configData.size();i++){
            PanelBindConfig config = configData.get(i);
            if(config.getBindSerialNo().equals(serialNo)){
                configData.remove(i);
                break;
            }
        }
    }

    private int checkSameDevice(String serialNo){
        for(int i = 0;i < configData.size();i++){
            PanelBindConfig config = configData.get(i);
            if(config.getBindSerialNo().equals(serialNo)){
                return i;
            }
        }
        return -1;
    }

    public int getDeviceBindType(DeviceBean deviceBean) {
        if(deviceBean.getDeviceGroup() == 1){
            return 12;
        }
        if ("breaker_light".equals(deviceBean.getDeviceType())){
            return 1;
        }else if ("adjustLight".equals(deviceBean.getDeviceType())){
            return 2;
        }else if ("airconditioner".equals(deviceBean.getType())){
            return 3;
        }else if ("freshair".equals(deviceBean.getType())){
            return 4;
        }else if ("curtain".equals(deviceBean.getType())){
            return 5;
        }else if ("adjustcolor".equals(deviceBean.getDeviceType())){
            return 6;
        }else if ("floorheating".equals(deviceBean.getType())){
            return 8;
        }else if ("tctdl".equals(deviceBean.getDeviceType()) || "adjustLCT".equals(deviceBean.getDeviceType())){
            return 9;
        }
        return -1;
    }

    public void onSelectedDevices(ArrayList<DeviceBean> selectedDevices){
        if(selectedDevices == null || selectedDevices.size() == 0){
            getTitleBar().getRightView().setEnabled(false);
        }else{
            getTitleBar().getRightView().setEnabled(true);
        }
    }

    @Override
    public void onRightClick(View view) {
        List<PanelBindConfig> cancelConfigs = new ArrayList<>();
        for(PanelBindConfig config : orignConfigData){
            Boolean isHave = false;
            for(PanelBindConfig config1 : configData){
                if(config.getBindSerialNo().equals(config1.getBindSerialNo()) && config.getPanelIndex() == config1.getPanelIndex()){
                    isHave =  true;
                    configData.remove(config1);
                    break;
                }
            }
            if(!isHave){
                cancelConfigs.add(config);
            }
        }
        cancelConfigs.addAll(configData);
        if(cancelConfigs.size() == 0){
            toast("当前绑定未产生变化，无需重新进行绑定");
        }else{
            Intent intent = new Intent(this, PanelBindOperationActivity.class);
            intent.putExtra("bingingDatas", (Serializable) cancelConfigs);
            startActivity(intent);
        }

    }
}
